﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using Maticsoft.Common;
using System.Drawing;
using LTP.Accounts.Bus;
namespace lcs.Web.materiallinkinfo
{
    public partial class List : Web.Admin.BasePage
    {
        
        
        
		lcs.BLL.materiallinkinfo bll = new lcs.BLL.materiallinkinfo();
        private DataTable dt = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {                
                BindData();
                DataBind();
            }
        }
        private void BindTree(DataTable dtSource, TreeNode parentNode, string parentID)
        {
            DataRow[] rows = dtSource.Select(string.Format("pmlid={0}", parentID));
            foreach (DataRow row in rows)
            {
                TreeNode node = new TreeNode();
                node.Text = row["mlname"].ToString()+":"+ row["mlnum"].ToString();
                node.Value = row["mlid"].ToString();
                BindTree(dtSource, node, row["mlid"].ToString());
                if (parentNode == null)
                {
                    treeMenu.Nodes.Add(node);
                }
                else
                {
                    parentNode.ChildNodes.Add(node);
                }
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            
            string mlname = this.txtmlname.Text;
            decimal mlnum = decimal.Parse(this.txtmlnum.Text);
            int pmlid = 0;
            if (treeMenu.SelectedValue!=null && treeMenu.SelectedValue!="")            
            int.TryParse(treeMenu.SelectedValue.ToString(),out pmlid);

            lcs.Model.materiallinkinfo model = new lcs.Model.materiallinkinfo();
            model.mlname = mlname;
            model.mlnum = mlnum;
            model.pmlid = pmlid;

            lcs.BLL.materiallinkinfo bll = new lcs.BLL.materiallinkinfo();
            bll.Add(model);
            BindData();
            Maticsoft.Common.MessageBox.Show(this, "保存成功！");

        }
        protected void btnSavexg_Click(object sender, EventArgs e)
        {

            string mlname = this.txtmlname.Text;
            decimal mlnum = decimal.Parse(this.txtmlnum.Text);
            int pmlid = 0;
            if (treeMenu.SelectedValue != null && treeMenu.SelectedValue != "")
                int.TryParse(treeMenu.SelectedValue.ToString(), out pmlid);

            lcs.Model.materiallinkinfo model = new lcs.Model.materiallinkinfo();
            model.mlname = mlname;
            model.mlnum = mlnum;
            model.mlid = pmlid;

            lcs.BLL.materiallinkinfo bll = new lcs.BLL.materiallinkinfo();
            bll.Update(model);
            BindData();
            Maticsoft.Common.MessageBox.Show(this, "保存成功！");

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int pmlid = 0;
            if (treeMenu.SelectedValue != null)
            { 
                int.TryParse(treeMenu.SelectedValue.ToString(), out pmlid);
            bll.Delete(pmlid);
            BindData();
            }
        }
        
        #region gridView
                        
        public void BindData()
        {
            treeMenu.Nodes.Clear();
            dt = new DataTable();
            dt = bll.GetAllList().Tables[0];
            BindTree(dt, null, "0");


        }

        

       

        #endregion





    }
}
